home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / interp / perl-5.003.tar.gz / perl-5.003.tar / perl-5.003 / lib / Getopt / Long.pm next >
Text File  |  1996-03-25  |  26KB  |  892 lines

  1. # GetOpt::Long.pm -- POSIX compatible options parsing
  2.  
  3. # RCS Status      : $Id: GetoptLong.pm,v 2.1 1996/02/02 20:24:35 jv Exp $
  4. # Author          : Johan Vromans
  5. # Created On      : Tue Sep 11 15:00:12 1990
  6. # Last Modified By: Johan Vromans
  7. # Last Modified On: Fri Feb  2 21:24:32 1996
  8. # Update Count    : 347
  9. # Status          : Released
  10.  
  11. package Getopt::Long;
  12. require 5.000;
  13. require Exporter;
  14.  
  15. @ISA = qw(Exporter);
  16. @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
  17. $VERSION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/);
  18. use strict;
  19.  
  20. =head1 NAME
  21.  
  22. GetOptions - extended processing of command line options
  23.  
  24. =head1 SYNOPSIS
  25.  
  26.   use Getopt::Long;
  27.   $result = GetOptions (...option-descriptions...);
  28.  
  29. =head1 DESCRIPTION
  30.  
  31. The Getopt::Long module implements an extended getopt function called
  32. GetOptions(). This function adheres to the POSIX syntax for command
  33. line options, with GNU extensions. In general, this means that options
  34. have long names instead of single letters, and are introduced with a
  35. double dash "--". There is no bundling of command line options, as was
  36. the case with the more traditional single-letter approach. For
  37. example, the UNIX "ps" command can be given the command line "option" 
  38.  
  39.   -vax
  40.  
  41. which means the combination of B<-v>, B<-a> and B<-x>. With the new
  42. syntax B<--vax> would be a single option, probably indicating a
  43. computer architecture. 
  44.  
  45. Command line options can be used to set values. These values can be
  46. specified in one of two ways:
  47.  
  48.   --size 24
  49.   --size=24
  50.  
  51. GetOptions is called with a list of option-descriptions, each of which
  52. consists of two elements: the option specifier and the option linkage.
  53. The option specifier defines the name of the option and, optionally,
  54. the value it can take. The option linkage is usually a reference to a
  55. variable that will be set when the option is used. For example, the
  56. following call to GetOptions:
  57.  
  58.   &GetOptions("size=i" => \$offset);
  59.  
  60. will accept a command line option "size" that must have an integer
  61. value. With a command line of "--size 24" this will cause the variable
  62. $offset to get the value 24.
  63.  
  64. Alternatively, the first argument to GetOptions may be a reference to
  65. a HASH describing the linkage for the options. The following call is
  66. equivalent to the example above:
  67.  
  68.   %optctl = ("size" => \$offset);
  69.   &GetOptions(\%optctl, "size=i");
  70.  
  71. Linkage may be specified using either of the above methods, or both.
  72. Linkage specified in the argument list takes precedence over the
  73. linkage specified in the HASH.
  74.  
  75. The command line options are taken from array @ARGV. Upon completion
  76. of GetOptions, @ARGV will contain the rest (i.e. the non-options) of
  77. the command line.
  78.  
  79. Each option specifier designates the name of the option, optionally
  80. followed by an argument specifier. Values for argument specifiers are:
  81.  
  82. =over 8
  83.  
  84. =item <none>
  85.  
  86. Option does not take an argument. 
  87. The option variable will be set to 1.
  88.  
  89. =item !
  90.  
  91. Option does not take an argument and may be negated, i.e. prefixed by
  92. "no". E.g. "foo!" will allow B<--foo> (with value 1) and B<-nofoo>
  93. (with value 0).
  94. The option variable will be set to 1, or 0 if negated.
  95.  
  96. =item =s
  97.  
  98. Option takes a mandatory string argument.
  99. This string will be assigned to the option variable.
  100. Note that even if the string argument starts with B<-> or B<-->, it
  101. will not be considered an option on itself.
  102.  
  103. =item :s
  104.  
  105. Option takes an optional string argument.
  106. This string will be assigned to the option variable.
  107. If omitted, it will be assigned "" (an empty string).
  108. If the string argument starts with B<-> or B<-->, it
  109. will be considered an option on itself.
  110.  
  111. =item =i
  112.  
  113. Option takes a mandatory integer argument.
  114. This value will be assigned to the option variable.
  115. Note that the value may start with B<-> to indicate a negative
  116. value. 
  117.  
  118. =item :i
  119.  
  120. Option takes an optional integer argument.
  121. This value will be assigned to the option variable.
  122. If omitted, the value 0 will be assigned.
  123. Note that the value may start with B<-> to indicate a negative
  124. value.
  125.  
  126. =item =f
  127.  
  128. Option takes a mandatory real number argument.
  129. This value will be assigned to the option variable.
  130. Note that the value may start with B<-> to indicate a negative
  131. value.
  132.  
  133. =item :f
  134.  
  135. Option takes an optional real number argument.
  136. This value will be assigned to the option variable.
  137. If omitted, the value 0 will be assigned.
  138.  
  139. =back
  140.  
  141. A lone dash B<-> is considered an option, the corresponding option
  142. name is the empty string.
  143.  
  144. A double dash on itself B<--> signals end of the options list.
  145.  
  146. =head2 Linkage specification
  147.  
  148. The linkage specifier is optional. If no linkage is explicitly
  149. specified but a ref HASH is passed, GetOptions will place the value in
  150. the HASH. For example:
  151.  
  152.   %optctl = ();
  153.   &GetOptions (\%optctl, "size=i");
  154.  
  155. will perform the equivalent of the assignment
  156.  
  157.   $optctl{"size"} = 24;
  158.  
  159. For array options, a reference to an array is used, e.g.:
  160.  
  161.   %optctl = ();
  162.   &GetOptions (\%optctl, "sizes=i@");
  163.  
  164. with command line "-sizes 24 -sizes 48" will perform the equivalent of
  165. the assignment
  166.  
  167.   $optctl{"sizes"} = [24, 48];
  168.  
  169. If no linkage is explicitly specified and no ref HASH is passed,
  170. GetOptions will put the value in a global variable named after the
  171. option, prefixed by "opt_". To yield a usable Perl variable,
  172. characters that are not part of the syntax for variables are
  173. translated to underscores. For example, "--fpp-struct-return" will set
  174. the variable $opt_fpp_struct_return. Note that this variable resides
  175. in the namespace of the calling program, not necessarily B<main>.
  176. For example:
  177.  
  178.   &GetOptions ("size=i", "sizes=i@");
  179.  
  180. with command line "-size 10 -sizes 24 -sizes 48" will perform the
  181. equivalent of the assignments
  182.  
  183.   $opt_size = 10;
  184.   @opt_sizes = (24, 48);
  185.  
  186. A lone dash B<-> is considered an option, the corresponding Perl
  187. identifier is $opt_ .
  188.  
  189. The linkage specifier can be a reference to a scalar, a reference to
  190. an array or a reference to a subroutine.
  191.  
  192. If a REF SCALAR is supplied, the new value is stored in the referenced
  193. variable. If the option occurs more than once, the previous value is
  194. overwritten. 
  195.  
  196. If a REF ARRAY is supplied, the new value is appended (pushed) to the
  197. referenced array. 
  198.  
  199. If a REF CODE is supplied, the referenced subroutine is called with
  200. two arguments: the option name and the option value.
  201. The option name is always the true name, not an abbreviation or alias.
  202.  
  203. =head2 Aliases and abbreviations
  204.  
  205. The option name may actually be a list of option names, separated by
  206. "|"s, e.g. "foo|bar|blech=s". In this example, "foo" is the true name
  207. op this option. If no linkage is specified, options "foo", "bar" and
  208. "blech" all will set $opt_foo.
  209.  
  210. Option names may be abbreviated to uniqueness, depending on
  211. configuration variable $Getopt::Long::autoabbrev.
  212.  
  213. =head2 Non-option call-back routine
  214.  
  215. A special option specifier, <>, can be used to designate a subroutine
  216. to handle non-option arguments. GetOptions will immediately call this
  217. subroutine for every non-option it encounters in the options list.
  218. This subroutine gets the name of the non-option passed.
  219. This feature requires $Getopt::Long::order to have the value $PERMUTE.
  220. See also the examples.
  221.  
  222. =head2 Option starters
  223.  
  224. On the command line, options can start with B<-> (traditional), B<-->
  225. (POSIX) and B<+> (GNU, now being phased out). The latter is not
  226. allowed if the environment variable B<POSIXLY_CORRECT> has been
  227. defined.
  228.  
  229. Options that start with "--" may have an argument appended, separated
  230. with an "=", e.g. "--foo=bar".
  231.  
  232. =head2 Return value
  233.  
  234. A return status of 0 (false) indicates that the function detected
  235. one or more errors.
  236.  
  237. =head1 COMPATIBILITY
  238.  
  239. Getopt::Long::GetOptions() is the successor of
  240. B<newgetopt.pl> that came with Perl 4. It is fully upward compatible.
  241. In fact, the Perl 5 version of newgetopt.pl is just a wrapper around
  242. the module.
  243.  
  244. If an "@" sign is appended to the argument specifier, the option is
  245. treated as an array.  Value(s) are not set, but pushed into array
  246. @opt_name. This only applies if no linkage is supplied.
  247.  
  248. If configuration variable $Getopt::Long::getopt_compat is set to a
  249. non-zero value, options that start with "+" may also include their
  250. arguments, e.g. "+foo=bar". This is for compatiblity with older
  251. implementations of the GNU "getopt" routine.
  252.  
  253. If the first argument to GetOptions is a string consisting of only
  254. non-alphanumeric characters, it is taken to specify the option starter
  255. characters. Everything starting with one of these characters from the
  256. starter will be considered an option. B<Using a starter argument is
  257. strongly deprecated.>
  258.  
  259. For convenience, option specifiers may have a leading B<-> or B<-->,
  260. so it is possible to write:
  261.  
  262.    GetOptions qw(-foo=s --bar=i --ar=s);
  263.  
  264. =head1 EXAMPLES
  265.  
  266. If the option specifier is "one:i" (i.e. takes an optional integer
  267. argument), then the following situations are handled:
  268.  
  269.    -one -two        -> $opt_one = '', -two is next option
  270.    -one -2        -> $opt_one = -2
  271.  
  272. Also, assume specifiers "foo=s" and "bar:s" :
  273.  
  274.    -bar -xxx        -> $opt_bar = '', '-xxx' is next option
  275.    -foo -bar        -> $opt_foo = '-bar'
  276.    -foo --        -> $opt_foo = '--'
  277.  
  278. In GNU or POSIX format, option names and values can be combined:
  279.  
  280.    +foo=blech        -> $opt_foo = 'blech'
  281.    --bar=        -> $opt_bar = ''
  282.    --bar=--        -> $opt_bar = '--'
  283.  
  284. Example of using variabel references:
  285.  
  286.    $ret = &GetOptions ('foo=s', \$foo, 'bar=i', 'ar=s', \@ar);
  287.  
  288. With command line options "-foo blech -bar 24 -ar xx -ar yy" 
  289. this will result in:
  290.  
  291.    $bar = 'blech'
  292.    $opt_bar = 24
  293.    @ar = ('xx','yy')
  294.  
  295. Example of using the <> option specifier:
  296.  
  297.    @ARGV = qw(-foo 1 bar -foo 2 blech);
  298.    &GetOptions("foo=i", \$myfoo, "<>", \&mysub);
  299.  
  300. Results:
  301.  
  302.    &mysub("bar") will be called (with $myfoo being 1)
  303.    &mysub("blech") will be called (with $myfoo being 2)
  304.  
  305. Compare this with:
  306.  
  307.    @ARGV = qw(-foo 1 bar -foo 2 blech);
  308.    &GetOptions("foo=i", \$myfoo);
  309.  
  310. This will leave the non-options in @ARGV:
  311.  
  312.    $myfoo -> 2
  313.    @ARGV -> qw(bar blech)
  314.  
  315. =head1 CONFIGURATION VARIABLES
  316.  
  317. The following variables can be set to change the default behaviour of
  318. GetOptions():
  319.  
  320. =over 12
  321.  
  322. =item $Getopt::Long::autoabbrev      
  323.  
  324. Allow option names to be abbreviated to uniqueness.
  325. Default is 1 unless environment variable
  326. POSIXLY_CORRECT has been set.
  327.  
  328. =item $Getopt::Long::getopt_compat   
  329.  
  330. Allow '+' to start options.
  331. Default is 1 unless environment variable
  332. POSIXLY_CORRECT has been set.
  333.  
  334. =item $Getopt::Long::order           
  335.  
  336. Whether non-options are allowed to be mixed with
  337. options.
  338. Default is $REQUIRE_ORDER if environment variable
  339. POSIXLY_CORRECT has been set, $PERMUTE otherwise.
  340.  
  341. $PERMUTE means that 
  342.  
  343.     -foo arg1 -bar arg2 arg3
  344.  
  345. is equivalent to
  346.  
  347.     -foo -bar arg1 arg2 arg3
  348.  
  349. If a non-option call-back routine is specified, @ARGV will always be
  350. empty upon succesful return of GetOptions since all options have been
  351. processed, except when B<--> is used:
  352.  
  353.     -foo arg1 -bar arg2 -- arg3
  354.  
  355. will call the call-back routine for arg1 and arg2, and terminate
  356. leaving arg2 in @ARGV.
  357.  
  358. If $Getopt::Long::order is $REQUIRE_ORDER, options processing
  359. terminates when the first non-option is encountered.
  360.  
  361.     -foo arg1 -bar arg2 arg3
  362.  
  363. is equivalent to
  364.  
  365.     -foo -- arg1 -bar arg2 arg3
  366.  
  367. $RETURN_IN_ORDER is not supported by GetOptions().
  368.  
  369. =item $Getopt::Long::ignorecase      
  370.  
  371. Ignore case when matching options. Default is 1.
  372.  
  373. =item $Getopt::Long::VERSION
  374.  
  375. The version number of this Getopt::Long implementation in the format
  376. C<major>.C<minor>. This can be used to have Exporter check the
  377. version, e.g.
  378.  
  379.     use Getopt::Long 2.00;
  380.  
  381. You can inspect $Getopt::Long::major_version and
  382. $Getopt::Long::minor_version for the individual components.
  383.  
  384. =item $Getopt::Long::error
  385.  
  386. Internal error flag. May be incremented from a call-back routine to
  387. cause options parsing to fail.
  388.  
  389. =item $Getopt::Long::debug           
  390.  
  391. Enable copious debugging output. Default is 0.
  392.  
  393. =back
  394.  
  395. =cut
  396.  
  397. ################ Introduction ################
  398. #
  399. # This package implements an extended getopt function. This function
  400. # adheres to the new syntax (long option names, no bundling). It tries
  401. # to implement the better functionality of traditional, GNU and POSIX
  402. # getopt functions.
  403. # This program is Copyright 1990,1996 by Johan Vromans.
  404. # This program is free software; you can redistribute it and/or
  405. # modify it under the terms of the GNU General Public License
  406. # as published by the Free Software Foundation; either version 2
  407. # of the License, or (at your option) any later version.
  408. # This program is distributed in the hope that it will be useful,
  409. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  410. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  411. # GNU General Public License for more details.
  412. # If you do not have a copy of the GNU General Public License write to
  413. # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
  414. # MA 02139, USA.
  415.  
  416. ################ History ################
  417. # 13-Jan-1996        Johan Vromans
  418. #    Generalized the linkage interface.
  419. #    Eliminated the linkage argument.
  420. #    Add code references as a possible value for the option linkage.
  421. #    Add option specifier <> to have a call-back for non-options.
  422. #
  423. # 26-Dec-1995        Johan Vromans
  424. #    Import from netgetopt.pl.
  425. #    Turned into a decent module.
  426. #    Added linkage argument.
  427.  
  428. ################ Configuration Section ################
  429.  
  430. # Values for $order. See GNU getopt.c for details.
  431. ($Getopt::Long::REQUIRE_ORDER,
  432.  $Getopt::Long::PERMUTE, 
  433.  $Getopt::Long::RETURN_IN_ORDER) = (0..2);
  434.  
  435. my $gen_prefix;            # generic prefix (option starters)
  436.  
  437. # Handle POSIX compliancy.
  438. if ( defined $ENV{"POSIXLY_CORRECT"} ) {
  439.     $gen_prefix = "(--|-)";
  440.     $Getopt::Long::autoabbrev = 0;    # no automatic abbrev of options
  441.     $Getopt::Long::getopt_compat = 0;    # disallow '+' to start options
  442.     $Getopt::Long::order = $Getopt::Long::REQUIRE_ORDER;
  443. }
  444. else {
  445.     $gen_prefix = "(--|-|\\+)";
  446.     $Getopt::Long::autoabbrev = 1;    # automatic abbrev of options
  447.     $Getopt::Long::getopt_compat = 1;    # allow '+' to start options
  448.     $Getopt::Long::order = $Getopt::Long::PERMUTE;
  449. }
  450.  
  451. # Other configurable settings.
  452. $Getopt::Long::debug = 0;        # for debugging
  453. $Getopt::Long::error = 0;        # error tally
  454. $Getopt::Long::ignorecase = 1;        # ignore case when matching options
  455. ($Getopt::Long::version,
  456.  $Getopt::Long::major_version, 
  457.  $Getopt::Long::minor_version) = '$Revision: 2.1 $ ' =~ /: ((\d+)\.(\d+))/;
  458. $Getopt::Long::version .= '*' if length('$Locker:  $ ') > 12;
  459.  
  460. ################ Subroutines ################
  461.  
  462. sub GetOptions {
  463.  
  464.     my @optionlist = @_;    # local copy of the option descriptions
  465.     my $argend = '--';        # option list terminator
  466.     my %opctl;            # table of arg.specs
  467.     my $pkg = (caller)[0];    # current context
  468.                 # Needed if linkage is omitted.
  469.     my %aliases;        # alias table
  470.     my @ret = ();        # accum for non-options
  471.     my %linkage;        # linkage
  472.     my $userlinkage;        # user supplied HASH
  473.     my $debug = $Getopt::Long::debug;    # convenience
  474.     my $genprefix = $gen_prefix; # so we can call the same module more 
  475.                 # than once in differing environments
  476.     $Getopt::Long::error = 0;
  477.  
  478.     print STDERR ("GetOptions $Getopt::Long::version",
  479.           " [GetOpt::Long $Getopt::Long::VERSION] -- ",
  480.           "called from package \"$pkg\".\n",
  481.           "  autoabbrev=$Getopt::Long::autoabbrev".
  482.           ",getopt_compat=$Getopt::Long::getopt_compat",
  483.           ",genprefix=\"$genprefix\"",
  484.           ",order=$Getopt::Long::order",
  485.           ",ignorecase=$Getopt::Long::ignorecase",
  486.           ".\n")
  487.     if $debug;
  488.  
  489.     # Check for ref HASH as first argument. 
  490.     $userlinkage = undef;
  491.     if ( ref($optionlist[0]) && ref($optionlist[0]) eq 'HASH' ) {
  492.     $userlinkage = shift (@optionlist);
  493.     }
  494.  
  495.     # See if the first element of the optionlist contains option
  496.     # starter characters.
  497.     if ( $optionlist[0] =~ /^\W+$/ ) {
  498.     $genprefix = shift (@optionlist);
  499.     # Turn into regexp.
  500.     $genprefix =~ s/(\W)/\\$1/g;
  501.     $genprefix = "[" . $genprefix . "]";
  502.     }
  503.  
  504.     # Verify correctness of optionlist.
  505.     %opctl = ();
  506.     while ( @optionlist > 0 ) {
  507.     my $opt = shift (@optionlist);
  508.  
  509.     # Strip leading prefix so people can specify "-foo=i" if they like.
  510.     $opt = $' if $opt =~ /^($genprefix)+/;
  511.  
  512.     if ( $opt eq '<>' ) {
  513.         if ( (defined $userlinkage)
  514.         && !(@optionlist > 0 && ref($optionlist[0]))
  515.         && (exists $userlinkage->{$opt})
  516.         && ref($userlinkage->{$opt}) ) {
  517.         unshift (@optionlist, $userlinkage->{$opt});
  518.         }
  519.         unless ( @optionlist > 0 
  520.             && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) {
  521.         warn ("Option spec <> requires a reference to a subroutine\n");
  522.         $Getopt::Long::error++;
  523.         next;
  524.         }
  525.         $linkage{'<>'} = shift (@optionlist);
  526.         next;
  527.     }
  528.  
  529.     $opt =~ tr/A-Z/a-z/ if $Getopt::Long::ignorecase;
  530.     if ( $opt !~ /^(\w+[-\w|]*)?(!|[=:][infse]@?)?$/ ) {
  531.         warn ("Error in option spec: \"", $opt, "\"\n");
  532.         $Getopt::Long::error++;
  533.         next;
  534.     }
  535.     my ($o, $c, $a) = ($1, $2);
  536.  
  537.     if ( ! defined $o ) {
  538.         # empty -> '-' option
  539.         $opctl{$o = ''} = defined $c ? $c : '';
  540.     }
  541.     else {
  542.         # Handle alias names
  543.         my @o =  split (/\|/, $o);
  544.         $o = $o[0];
  545.         foreach ( @o ) {
  546.         if ( defined $c && $c eq '!' ) {
  547.             $opctl{"no$_"} = $c;
  548.             $c = '';
  549.         }
  550.         $opctl{$_} = defined $c ? $c : '';
  551.         if ( defined $a ) {
  552.             # Note alias.
  553.             $aliases{$_} = $a;
  554.         }
  555.         else {
  556.             # Set primary name.
  557.             $a = $_;
  558.         }
  559.         }
  560.     }
  561.  
  562.     # If no linkage is supplied in the @optionlist, copy it from
  563.     # the userlinkage if available.
  564.     if ( defined $userlinkage ) {
  565.         unless ( @optionlist > 0 && ref($optionlist[0]) ) {
  566.         if ( exists $userlinkage->{$o} && ref($userlinkage->{$o}) ) {
  567.             print STDERR ("=> found userlinkage for \"$o\": ",
  568.                   "$userlinkage->{$o}\n")
  569.             if $debug;
  570.             unshift (@optionlist, $userlinkage->{$o});
  571.         }
  572.         else {
  573.             # Do nothing. Being undefined will be handled later.
  574.             next;
  575.         }
  576.         }
  577.     }
  578.  
  579.     # Copy the linkage. If omitted, link to global variable.
  580.     if ( @optionlist > 0 && ref($optionlist[0]) ) {
  581.         print STDERR ("=> link \"$o\" to $optionlist[0]\n")
  582.         if $debug;
  583.         if ( ref($optionlist[0]) eq 'SCALAR'
  584.         || ref($optionlist[0]) eq 'ARRAY'
  585.         || ref($optionlist[0]) eq 'CODE' ) {
  586.         $linkage{$o} = shift (@optionlist);
  587.         }
  588.         else {
  589.         warn ("Invalid option linkage for \"", $opt, "\"\n");
  590.         $Getopt::Long::error++;
  591.         }
  592.     }
  593.     else {
  594.         # Link to global $opt_XXX variable.
  595.         # Make sure a valid perl identifier results.
  596.         my $ov = $o;
  597.         $ov =~ s/\W/_/g;
  598.         if ( $c && $c =~ /@/ ) {
  599.         print STDERR ("=> link \"$o\" to \@$pkg","::opt_$ov\n")
  600.             if $debug;
  601.         eval ("\$linkage{\$o} = \\\@".$pkg."::opt_$ov;");
  602.         }
  603.         else {
  604.         print STDERR ("=> link \"$o\" to \$$pkg","::opt_$ov\n")
  605.             if $debug;
  606.         eval ("\$linkage{\$o} = \\\$".$pkg."::opt_$ov;");
  607.         }
  608.     }
  609.     }
  610.  
  611.     # Bail out if errors found.
  612.     return 0 if $Getopt::Long::error;
  613.  
  614.     # Sort the possible option names.
  615.     my @opctl = sort(keys (%opctl)) if $Getopt::Long::autoabbrev;
  616.  
  617.     # Show if debugging.
  618.     if ( $debug ) {
  619.     my ($arrow, $k, $v);
  620.     $arrow = "=> ";
  621.     while ( ($k,$v) = each(%opctl) ) {
  622.         print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n");
  623.         $arrow = "   ";
  624.     }
  625.     }
  626.  
  627.     my $opt;            # current option
  628.     my $arg;            # current option value
  629.     my $array;            # current option is array typed
  630.  
  631.     # Process argument list
  632.     while ( @ARGV > 0 ) {
  633.  
  634.     # >>> See also the continue block <<<
  635.  
  636.     #### Get next argument ####
  637.  
  638.     $opt = shift (@ARGV);
  639.     $arg = undef;
  640.     my $optarg = undef;
  641.     $array = 0;
  642.     print STDERR ("=> option \"", $opt, "\"\n") if $debug;
  643.  
  644.     #### Determine what we have ####
  645.  
  646.     # Double dash is option list terminator.
  647.     if ( $opt eq $argend ) {
  648.         # Finish. Push back accumulated arguments and return.
  649.         unshift (@ARGV, @ret) 
  650.         if $Getopt::Long::order == $Getopt::Long::PERMUTE;
  651.         return ($Getopt::Long::error == 0);
  652.     }
  653.  
  654.     if ( $opt =~ /^$genprefix/ ) {
  655.         # Looks like an option.
  656.         $opt = $';        # option name (w/o prefix)
  657.         # If it is a long opt, it may include the value.
  658.         if (($& eq "--" || ($Getopt::Long::getopt_compat && $& eq "+"))
  659.         && $opt =~ /^([^=]+)=/ ) {
  660.         $opt = $1;
  661.         $optarg = $';
  662.         print STDERR ("=> option \"", $opt, 
  663.                   "\", optarg = \"$optarg\"\n") if $debug;
  664.         }
  665.  
  666.     }
  667.  
  668.     # Not an option. Save it if we $PERMUTE and don't have a <>.
  669.     elsif ( $Getopt::Long::order == $Getopt::Long::PERMUTE ) {
  670.         # Try non-options call-back.
  671.         my $cb;
  672.         if ( (defined ($cb = $linkage{'<>'})) ) {
  673.         &$cb($opt);
  674.         }
  675.         else {
  676.         push (@ret, $opt);
  677.         }
  678.         next;
  679.     }
  680.  
  681.     # ...otherwise, terminate.
  682.     else {
  683.         # Push this one back and exit.
  684.         unshift (@ARGV, $opt);
  685.         return ($Getopt::Long::error == 0);
  686.     }
  687.  
  688.     #### Look it up ###
  689.  
  690.     $opt =~ tr/A-Z/a-z/ if $Getopt::Long::ignorecase;
  691.  
  692.     my $tryopt = $opt;
  693.     if ( $Getopt::Long::autoabbrev ) {
  694.         my $pat;
  695.  
  696.         # Turn option name into pattern.
  697.         ($pat = $opt) =~ s/(\W)/\\$1/g;
  698.         # Look up in option names.
  699.         my @hits = grep (/^$pat/, @opctl);
  700.         print STDERR ("=> ", 0+@hits, " hits (@hits) with \"$pat\" ",
  701.               "out of ", 0+@opctl, "\n") if $debug;
  702.  
  703.         # Check for ambiguous results.
  704.         unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
  705.         print STDERR ("Option ", $opt, " is ambiguous (",
  706.                   join(", ", @hits), ")\n");
  707.         $Getopt::Long::error++;
  708.         next;
  709.         }
  710.  
  711.         # Complete the option name, if appropriate.
  712.         if ( @hits == 1 && $hits[0] ne $opt ) {
  713.         $tryopt = $hits[0];
  714.         print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
  715.             if $debug;
  716.         }
  717.     }
  718.  
  719.     my $type;
  720.     unless  ( defined ( $type = $opctl{$tryopt} ) ) {
  721.         print STDERR ("Unknown option: ", $opt, "\n");
  722.         $Getopt::Long::error++;
  723.         next;
  724.     }
  725.     $opt = $tryopt;
  726.     print STDERR ("=> found \"$type\" for ", $opt, "\n") if $debug;
  727.  
  728.     #### Determine argument status ####
  729.  
  730.     # If it is an option w/o argument, we're almost finished with it.
  731.     if ( $type eq '' || $type eq '!' ) {
  732.         if ( defined $optarg ) {
  733.         print STDERR ("Option ", $opt, " does not take an argument\n");
  734.         $Getopt::Long::error++;
  735.         }
  736.         elsif ( $type eq '' ) {
  737.         $arg = 1;        # supply explicit value
  738.         }
  739.         else {
  740.         substr ($opt, 0, 2) = ''; # strip NO prefix
  741.         $arg = 0;        # supply explicit value
  742.         }
  743.         next;
  744.     }
  745.  
  746.     # Get mandatory status and type info.
  747.     my $mand;
  748.     ($mand, $type, $array) = $type =~ /^(.)(.)(@?)$/;
  749.  
  750.     # Check if there is an option argument available.
  751.     if ( defined $optarg ? ($optarg eq '') : (@ARGV <= 0) ) {
  752.  
  753.         # Complain if this option needs an argument.
  754.         if ( $mand eq "=" ) {
  755.         print STDERR ("Option ", $opt, " requires an argument\n");
  756.         $Getopt::Long::error++;
  757.         }
  758.         if ( $mand eq ":" ) {
  759.         $arg = $type eq "s" ? '' : 0;
  760.         }
  761.         next;
  762.     }
  763.  
  764.     # Get (possibly optional) argument.
  765.     $arg = defined $optarg ? $optarg : shift (@ARGV);
  766.  
  767.     #### Check if the argument is valid for this option ####
  768.  
  769.     if ( $type eq "s" ) {    # string
  770.         # A mandatory string takes anything. 
  771.         next if $mand eq "=";
  772.  
  773.         # An optional string takes almost anything. 
  774.         next if defined $optarg;
  775.         next if $arg eq "-";
  776.  
  777.         # Check for option or option list terminator.
  778.         if ($arg eq $argend ||
  779.         $arg =~ /^$genprefix.+/) {
  780.         # Push back.
  781.         unshift (@ARGV, $arg);
  782.         # Supply empty value.
  783.         $arg = '';
  784.         }
  785.         next;
  786.     }
  787.  
  788.     if ( $type eq "n" || $type eq "i" ) { # numeric/integer
  789.         if ( $arg !~ /^-?[0-9]+$/ ) {
  790.         if ( defined $optarg || $mand eq "=" ) {
  791.             print STDERR ("Value \"", $arg, "\" invalid for option ",
  792.                   $opt, " (number expected)\n");
  793.             $Getopt::Long::error++;
  794.             undef $arg;    # don't assign it
  795.         }
  796.         else {
  797.             # Push back.
  798.             unshift (@ARGV, $arg);
  799.             # Supply default value.
  800.             $arg = 0;
  801.         }
  802.         }
  803.         next;
  804.     }
  805.  
  806.     if ( $type eq "f" ) { # fixed real number, int is also ok
  807.         if ( $arg !~ /^-?[0-9.]+$/ ) {
  808.         if ( defined $optarg || $mand eq "=" ) {
  809.             print STDERR ("Value \"", $arg, "\" invalid for option ",
  810.                   $opt, " (real number expected)\n");
  811.             $Getopt::Long::error++;
  812.             undef $arg;    # don't assign it
  813.         }
  814.         else {
  815.             # Push back.
  816.             unshift (@ARGV, $arg);
  817.             # Supply default value.
  818.             $arg = 0.0;
  819.         }
  820.         }
  821.         next;
  822.     }
  823.  
  824.     die ("GetOpt::Long internal error (Can't happen)\n");
  825.     }
  826.  
  827.     continue {
  828.     if ( defined $arg ) {
  829.         $opt = $aliases{$opt} if defined $aliases{$opt};
  830.  
  831.         if ( defined $linkage{$opt} ) {
  832.         print STDERR ("=> ref(\$L{$opt}) -> ",
  833.                   ref($linkage{$opt}), "\n") if $debug;
  834.  
  835.         if ( ref($linkage{$opt}) eq 'SCALAR' ) {
  836.             print STDERR ("=> \$\$L{$opt} = \"$arg\"\n") if $debug;
  837.             ${$linkage{$opt}} = $arg;
  838.         }
  839.         elsif ( ref($linkage{$opt}) eq 'ARRAY' ) {
  840.             print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
  841.             if $debug;
  842.             push (@{$linkage{$opt}}, $arg);
  843.         }
  844.         elsif ( ref($linkage{$opt}) eq 'CODE' ) {
  845.             print STDERR ("=> &L{$opt}(\"$opt\", \"$arg\")\n")
  846.             if $debug;
  847.             &{$linkage{$opt}}($opt, $arg);
  848.         }
  849.         else {
  850.             print STDERR ("Invalid REF type \"", ref($linkage{$opt}),
  851.                   "\" in linkage\n");
  852.             die ("Getopt::Long -- internal error!\n");
  853.         }
  854.         }
  855.         # No entry in linkage means entry in userlinkage.
  856.         elsif ( $array ) {
  857.         if ( defined $userlinkage->{$opt} ) {
  858.             print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n")
  859.             if $debug;
  860.             push (@{$userlinkage->{$opt}}, $arg);
  861.         }
  862.         else {
  863.             print STDERR ("=>\$L{$opt} = [\"$arg\"]\n")
  864.             if $debug;
  865.             $userlinkage->{$opt} = [$arg];
  866.         }
  867.         }
  868.         else {
  869.         print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug;
  870.         $userlinkage->{$opt} = $arg;
  871.         }
  872.     }
  873.     }
  874.  
  875.     # Finish.
  876.     if ( $Getopt::Long::order == $Getopt::Long::PERMUTE ) {
  877.     #  Push back accumulated arguments
  878.     unshift (@ARGV, @ret) if @ret > 0;
  879.     }
  880.  
  881.     return ($Getopt::Long::error == 0);
  882. }
  883.  
  884. ################ Package return ################
  885.  
  886. # Returning 1 is so boring...
  887. $Getopt::Long::major_version * 1000 + $Getopt::Long::minor_version;
  888.